package com.busibuilding.administrate.area.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import com.busibuilding.administrate.area.dao.CityDAO;
import com.busibuilding.administrate.area.vo.CityCVO;
import com.busibuilding.common.config.sqlmap.entities.CityVO;
import com.busibuilding.common.exceptions.DAOException;
import com.busibuilding.common.exceptions.ServiceException;
import com.busibuilding.common.services.CommonServicesImpl;
import com.busibuilding.common.vo.ICommonVO;

/**
 * Class Name: CityServiceImpl.java
 * Description: 
 * @author Lin Yanjun
 * @version 1.0
 * @since 1.0
 * Date: 2010-3-3
 */

@Transactional(rollbackForClassName={"Exception"})
public class CityServiceImpl extends CommonServicesImpl implements CityService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5272365476537976647L;
	private static Logger logger = LogManager.getLogger(CityServiceImpl.class);
	
	private CityDAO cityDAO;

	public void setCityDAO(CityDAO cityDAO) {
		this.cityDAO = cityDAO;
	}

	@Override
	public int getRecordsCount(ICommonVO vo) throws ServiceException {
		int id = SQLMAP_COMMON_RESULT_ERROR;
		try {
			id = cityDAO.getRecordsCount(vo);
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_SEARCH_CITY, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_SEARCH_CITY, e);
		}
		return id;
	}

	@Override
	public int createCity(ICommonVO vo) throws ServiceException {
		int id = SQLMAP_COMMON_RESULT_ERROR;
		try {
			id = cityDAO.insert(vo);
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_INSERT_CITY, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_INSERT_CITY, e);
		}
		return id;
	}

	@Override
	public int deleteCity(ICommonVO vo) throws ServiceException {
		int result = SQLMAP_COMMON_RESULT_ERROR;
		try {
			result = cityDAO.update(vo);
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_UPDATE_CITY, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_UPDATE_CITY, e);
		}
		return result;
	}

	@Override
	public CityVO getCity(ICommonVO vo) throws ServiceException {
		CityCVO cityCVO = new CityCVO();
		try {
			cityCVO = cityDAO.getCity(vo);	
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_SEARCH_CITY, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_SEARCH_CITY, e);
		}
		return cityCVO;
	}
	@Override
	public List<CityCVO> searchCity(ICommonVO vo) throws ServiceException {
		List<CityCVO> resultList = new ArrayList<CityCVO>();
		try {
			resultList = cityDAO.searchCityByCondition(vo);			
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_SEARCH_CITY, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_SEARCH_CITY, e);
		}
		return resultList;
	}

	@Override
	public List<CityCVO> searchCityByPagination(ICommonVO vo)
			throws ServiceException {
		List<CityCVO> resultList = new ArrayList<CityCVO>();
		try {
			resultList = cityDAO.searchCityByCondition(vo, vo.getMinIndex(), vo.getMaxIndex());			
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_SEARCH_CITY, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_SEARCH_CITY, e);
		}
		return resultList;
	}

	@Override
	public int updateCity(ICommonVO vo) throws ServiceException {
		int result = SQLMAP_COMMON_RESULT_ERROR;
		try {
			result = cityDAO.update(vo);
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_UPDATE_CITY, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_UPDATE_CITY, e);
		}
		return result;
	}

}
